package com.kirayous.blog.controller;

import com.kirayous.api.blog.service.BlogInfoService;
import com.kirayous.api.service.impl.WebSocketServiceImpl;
import com.kirayous.api.strategy.factory.UploadStrategyFactory;
import com.kirayous.common.Result;
import com.kirayous.common.annotation.OptLog;
import com.kirayous.common.constant.OptTypeConst;
import com.kirayous.common.enums.FilePathEnum;
import com.kirayous.common.vo.VoiceVO;
import com.kirayous.common.vo.WebsiteConfigVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author KiraYous
 * @version V1.0
 * @Package com.kirayous.blog.controller
 * @date 2021/9/18 10:08
 */
@RestController
public class BlogInfoController {

    @Autowired
    BlogInfoService blogInfoService;

    @Autowired
    WebSocketServiceImpl webSocketService;






    /**
     *
     *
     * @param notice
     * @return result
     * 【后台】已被废止
     */
    @OptLog(optType = OptTypeConst.UPDATE)
    @ApiOperation(value = "修改公告-后台")
    @PutMapping("/admin/notice")
    public Result updateNotice(@RequestParam(value = "notice",required = true) String notice) {
        blogInfoService.updateNotice(notice);
        return Result.success().setMessage("修改成功！");
    }

    /**m
     *
     * @return Resutl
     *
     * 【后台】已被废止
     */
    @ApiOperation(value = "查看公告-后台")
    @GetMapping("/admin/notice")
    public Result getNotice() {
        String res=blogInfoService.getNotice();
        return Result.success().setMessage("查询成功").setData(res);
    }


    /**
     *
     * @return Result
     * 【前台】
     */
    @ApiOperation(value = "查看博客信息-前台")
    @GetMapping("/")
    public Result getBlogHomeInfo() {
        return Result.success().setData(blogInfoService.getBlogHomeInfo());
    }



    /**
     * 查看后台信息
     * // TODO 【后台】
     * @return  后台信息
     */
    @ApiOperation(value = "查看后台信息")
    @GetMapping("/admin")
    public Result getBlogBackInfo() {
        return Result.success(blogInfoService.getBlogBackInfo());
    }



    /**
     * 上传访客信息
     *
     * @return {@link Result}
     * 【后台】
     */
    @PostMapping("/report")
    public Result  report() {
        blogInfoService.report();
        return Result.success();
    }

    /**
     * 更新网站配置
     *
     * @param websiteConfigVO 网站配置信息
     * @return {@link Result}
     *
     * 【后台】
     */
    @ApiOperation(value = "更新网站配置")
    @PutMapping("/admin/website/config")
    public Result updateWebsiteConfig(@Valid @RequestBody WebsiteConfigVO websiteConfigVO) {
        blogInfoService.updateWebsiteConfig(websiteConfigVO);
        return Result.success();
    }

    /**
     * 获取网站配置
     *
     * @return {@link Result<WebsiteConfigVO>} 网站配置
     *
     * 【后台】
     */
    @ApiOperation(value = "获取网站配置")
    @GetMapping("/admin/website/config")
    public Result getWebsiteConfig() {
        return Result.success(blogInfoService.getWebsiteConfig());
    }



    /**
     * 上传博客配置图片
     *
     * @param file 文件
     * @return {@link Result<String>} 博客配置图片
     * 【后台】
     */
    @ApiOperation(value = "上传博客配置图片")
    @ApiImplicitParam(name = "file", value = "图片", required = true, dataType = "MultipartFile")
    @PostMapping("/admin/config/images")
    public Result savePhotoAlbumCover(MultipartFile file) {
        return Result.success(UploadStrategyFactory.executeUploadStrategy(file,FilePathEnum.CONFIG.getPath() ));
    }


    /**
     * 保存语音信息
     *
     * @param voiceVO 语音信息
     * @return {@link Result<String>} 语音地址
     *
     * 【前台】
     */
    @ApiOperation(value = "上传语音")
    @PostMapping("/voice")
    public Result sendVoice(VoiceVO voiceVO) {
        webSocketService.sendVoice(voiceVO);
        return Result.success();
    }

}
